﻿using System.Collections.Generic;
using System.Web.Mvc;
using SERVICES;
using SERVICES.Services;
using WMILK_MVC4.Helper;
using WMILK_MVC4.Models;

namespace WMILK_MVC4.Controllers
{
    public partial class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            return View();
        }

        /// <summary>
        /// return view contain catalog menu with subcatalog
        /// </summary>
        /// <returns></returns>
        public ActionResult CatalogMenu()
        {
            var catalogs = CatalogService.GetAllCatalogs();
            var catalogModels = new List<CatalogModel>();
            foreach (var catalog in catalogs)
            {
                var newCatalogModel = new CatalogModel(catalog);
                newCatalogModel.InitializeSubCatalogs();
                catalogModels.Add(newCatalogModel);
            }
            return PartialView(catalogModels);
        }

        public ActionResult MainMenu()
        {
            var subCatalogs = SubCatalogService.GetAllSubCatalog();
            var subCatalogsModels = new List<SubCatalogModel>();

            foreach (var subcatalog in subCatalogs)
            {
                var newSubCatalogsModel = new SubCatalogModel(subcatalog);
                newSubCatalogsModel.InitializeCatalogs();
                subCatalogsModels.Add(newSubCatalogsModel);
            }
            return PartialView(subCatalogsModels);
        }

        public ActionResult New()
        {
            return PartialView();
        }

     public ActionResult ProductDetail(int id)
        {
            var productDetail = ProductServices.GetProductByProductId(id);
            var productDetailModel = new ProductModel(productDetail);
            return View(productDetailModel);
        }
        /// <summary>
        /// Return view containt HotProduct list
        /// </summary>
        /// <returns></returns>
        public ActionResult HotProducts()
        {
            // this function missing scope of code define condition for hot products
            // this function miss scope of code determine hot products
            List<PRODUCT> products = ProductServices.GetHotProductList(NumberOfProductsInAPageHotProduct);
            ViewBag.NumberOfPages = PaginationHelper.CaculateNumberOfPage(ProductServices.NumberOfProducts(),
                NumberOfProductsInAPageHotProduct);
            return PartialView(ProductDetailHelper.ToProductDetailModelList(products));
        }

        public ActionResult HotproductForDetail()
        {
            List<PRODUCT> products = ProductServices.GetHotProductList(NumberOfProductsInAPageHotProduct);

            return PartialView(ProductDetailHelper.ToProductDetailModelList(products));
        }

        /// <summary>
        /// return view containt Hot Products with pagination. it is used by ajax.
        /// </summary>
        /// <param name="page">page index is defined by user</param>
        /// <returns></returns>
        public JsonResult HotProductsWithPagination(string page)
        {
            int pageIndex;
            pageIndex = int.TryParse(page, out pageIndex) ? pageIndex : 1;
            // still miss scope of code define conditon for hot products
            List<PRODUCT> products = ProductServices.GetHotProductsListWithPagination(pageIndex, NumberOfProductsInAPageHotProduct);

            return Json(ProductDetailHelper.ToProductDetailModelList(products), JsonRequestBehavior.AllowGet);
        }
        

        /// <summary>
        /// implement newProducts partial view for index page
        /// </summary>
        /// <returns></returns>
        public ActionResult NewProducts()
        {   
            var products = ProductServices.GetNewProductList(NumberOfProductsInAPageNewProduct);
            ViewBag.Header = "Sản Phẩm mới";
            ViewBag.CarouselSliderID = "NewProductCarouselSlider";
            
            return PartialView(ProductDetailHelper.ToProductDetailModelList(products));
        }

        public ActionResult ProductSuggestions()
        {
            List<PRODUCT> products = ProductServices.GetRandomProductList(6);

            return PartialView(ProductDetailHelper.ToProductDetailModelList(products));
        }

        public ActionResult NewProductsWithPagination(string page)
        {
            int pageIndex;
            pageIndex = int.TryParse(page, out pageIndex) ? pageIndex : 1;
            var products = ProductServices.GetNewProductsListWithPagination(pageIndex, NumberOfProductsInAPageNewProduct);
            return PartialView("listProductsInCarouselSlider", ProductDetailHelper.ToProductDetailModelList(products));
        }

        /// <summary>
        /// return view with promotion products list
        /// </summary>
        /// <returns></returns>
        public ActionResult PromotionProducts()
        {            
            var promotionProducts = ProductServices.GetPromotionProductsList(NumberOfProductsInAPagePromotionProduct);
            ViewBag.Header = "Sản Phẩm khuyến mãi";
            ViewBag.CarouselSliderID = "PromotionProductCarouselSlider";
            
            return PartialView(ProductDetailHelper.ToProductDetailModelList(promotionProducts));
        }

        public ActionResult PromotionProductsWithPagination(string page)
        {
            int pageIndex;
            pageIndex = int.TryParse(page, out pageIndex) ? pageIndex : 1;
            var products = ProductServices.GetPromotionProductsListWithPagination(pageIndex, NumberOfProductsInAPageNewProduct);
            return PartialView("listProductsInCarouselSlider", ProductDetailHelper.ToProductDetailModelList(products));
        }

        public ActionResult PromotionProductsForDetail()
        {
            var numberOfProducts = ProductServices.NumberOfPromotionProducts();
            var promotionProducts = ProductServices.GetPromotionProductsList(NumberOfProductsInAPagePromotionProduct);
            ViewBag.NumberOfPage = PaginationHelper.CaculateNumberOfPage(numberOfProducts, NumberOfProductsInAPagePromotionProduct);
            return PartialView("PromotionProductsForDetail", ProductDetailHelper.ToProductDetailModelList(promotionProducts));
        }

        /// <summary>
        /// return view with many Catalog's Products list
        /// </summary>
        /// <returns></returns>
        public ActionResult ListOfProductsInTheCatalogs()
        {
            var catalogs = CatalogService.GetTopCatalogs(4);
            var catalogModels = new List<CatalogModel>();
            foreach (var catalog in catalogs)
            { 
                var newCatalogModel = new CatalogModel(catalog);
                newCatalogModel.InitializeProducts(NumberOfProductsInAPageProductsByCatalog);
                catalogModels.Add(newCatalogModel);
            }
            return PartialView(catalogModels);
        }

        /// <summary>
        /// return view containt Products by Catalog with pagination. it is used by ajax.
        /// </summary>
        /// <param name="catalogId"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult ListOfProductsInTheCatalogsWithPagination(string catalogId, string page)
        {
            int iCatalogId;
            int iPage;
            if (!int.TryParse(catalogId, out iCatalogId) || !int.TryParse(page, out iPage))
                return null;
            var products = ProductServices.GetProductsListWithPagination(iCatalogId, iPage, NumberOfProductsInAPageProductsByCatalog);
            return PartialView(ProductDetailHelper.ToProductDetailModelList(products));
        }

               

        private const int NumberOfProductsInAPageHotProduct = 8;
        private const int NumberOfProductsInAPageNewProduct = 8;
        private const int NumberOfProductsInAPagePromotionProduct = 8;
        private const int NumberOfProductsInAPageProductsByCatalog = 8;
    }


}
